﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.Data.Entity;
using Top.Api;
using System.Data;
using Top.Api.Domain;

namespace TopActivityLibrary
{
    public sealed class TopContextActivity<T> : CodeActivity
        where T : TopObject
    {
        public InArgument<List<T>> EntityList { get; set; }
        public OutArgument<int> RecordsAffected { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            List<T> entityList = context.GetValue(this.EntityList);
            int recordsAffected = 0;
            if (entityList != null)
            {
                using (var db = new TopContext())
                {
                    foreach (T item in entityList)
                    {
                        //try
                        //{
                            //if(db.Set<T>().Contains<T>(item))
                            //if (db.Set<T>().Contains<T>(item, new TopEqualityComparer<T>()))
                            if (item.GetType().Name == "Item" || item.GetType().Name == "Trade")
                            {
                                long numIid = Convert.ToInt64(item.GetType().GetProperty("NumIid").GetValue(item, null));
                                if (db.Set<T>().Find(numIid) != null)
                                {
                                    db.Entry(item).State = EntityState.Modified;
                                }
                                else
                                {
                                    db.Entry(item).State = EntityState.Added;
                                }
                            }
                        //}
                        //catch(Exception ex) {
                        //    throw ex;
                        //}
                    }
                    recordsAffected = db.SaveChanges();
                }
                context.SetValue(this.RecordsAffected, recordsAffected);
            }
        }
    }

    class TopEqualityComparer<T> : IEqualityComparer<T>
    {
        public virtual bool Equals(T x, T y)
        {
            throw new NotImplementedException();
        }

        public virtual int GetHashCode(T obj)
        {
            throw new NotImplementedException();
        }
    }

    class ItemEqualityComparer : TopEqualityComparer<Item>
    {
        public override bool Equals(Item x, Item y)
        {
            if (x.NumIid == y.NumIid)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public override int GetHashCode(Item obj)
        {
            return obj.NumIid.GetHashCode();
        }
    }

    class TradeEqualityComparer : TopEqualityComparer<Trade>
    {
        public override bool Equals(Trade x, Trade y)
        {
            if (x.NumIid == y.NumIid)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public override int GetHashCode(Trade obj)
        {
            return obj.NumIid.GetHashCode();
        }
    }


}
